package com.shtel.paas.monitorcenter.process.management.controller;

import com.alibaba.fastjson.JSON;
import com.shtel.paas.monitorcenter.process.management.entity.AlarmFrequencyEntity;
import com.shtel.paas.monitorcenter.process.management.entity.HostInformation;
import com.shtel.paas.monitorcenter.process.management.entity.ProcessInfoParam;
import com.shtel.paas.monitorcenter.process.management.entity.ProcessInforEntity;
import com.shtel.paas.monitorcenter.process.management.service.ProcessInfoDataService;
import com.shtel.paas.monitorcenter.process.management.service.ProcessInfoService;
import org.apache.commons.lang.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
 * @ClassName
 * @Description Too
 * @Author zhoujie
 * @Data 2018/06/20 17:47
 **/
@MapperScan("com.shtel.paas.monitorcenter.process.management.mappper")

@RestController
public class ProcessCollectingController{

    @Autowired
    private ProcessInfoService service;



    @Autowired
    private ProcessInfoDataService processInfoDataService;

    @Autowired
    private AlarmFrequencyEntity alarmFrequencyEntity;




    /**
     *@Description 根据IP，port查询进程信息
     *@Param  ip，port
     *@return  ProcessInfoParam
     *@Author  zhoujie
     *@Date  2018/6/27 22:59
     *@修改人和其它信息
     */
    @RequestMapping(value = "/getProcessConfig")
    public List selectProcessConfig(@RequestParam ("ip")String ip,
                                    @RequestParam("port")String port)
    {
        System.out.println("ip: "+ip+"port:"+port);
        ProcessInfoParam param = new ProcessInfoParam(ip,port);

        return service.selectProcess(param);
    }

    @RequestMapping(value = "/getCommandConfig")
    public String selectCommand(@RequestParam ("ip")String ip,
                                @RequestParam("port")String port,
                                @RequestParam("type")String type ){
        port=unicodeToString(port);
        System.out.println("ip: "+ip+"port:"+port+"type"+type);

        return  service.selectCommand(ip,port,type);

    }


//    /**
//     *@Description 根据IP，pid查询进程信息
//     *@Param  [pid, id]
//     *@return  ProcessInfoParam
//     *@Author  zhojuie
//     *@Date  2018/6/27 15:33
//     */
//     @RequestMapping(value = "/getProcessConfigByPid")
//     @ResponseBody
//     public List<ProcessInfoParam> selectProcessByPID(@Param("pid")String pid,
//                                                      @Param("ip")String ip){
//         System.out.println("==================="+"pid:"+pid+"ip:"+ip+"=====================");
//         ProcessInfoParam param = new ProcessInfoParam(ip,"",pid);
//
//         return service.selectProcess(param);
//
//    }

     /**
      *@Description 若传入的ip值为空，查询alarm_process表中全部数据 else 根据ip查询数据
      *@Param ip
      *@return ProcessInforEntities
      *@Author  zhoujie
      *@Date  2018/6/27 15:54
      */
    @GetMapping("/getProcessAll")
    public String getAllInfor(@RequestParam("ip")String ip){
        System.out.println("=============="+ip+"==================");
        if(StringUtils.isBlank(ip)){
            List<ProcessInforEntity> processInforEntities = processInfoDataService.selectAllProccessInfo();
            String processInforEntitiesStr = JSON.toJSONString(processInforEntities);
            return processInforEntitiesStr;
        }else{
            List<ProcessInforEntity> processInforEntities = processInfoDataService.selectSingleInfo(ip);
            System.out.println(processInforEntities);
            return JSON.toJSONString(processInforEntities);
        }

    }



     /**

      *@Description 批量新增 alarm_process数据
      *@Param  List<ProcessInforEntity>
      *@return  插入成功数据量
      *@Author  zhoujie
      *@Date  2018/6/27 15:56
      *@修改人和其它信息
      */
    @RequestMapping(value="/insertProcess")
    public int insertProcessInfo(@RequestParam("id")String id,
                                 @RequestParam("hostid")String hostid,
                                 @RequestParam("ip")String ip,
                                 @RequestParam("command")String command,
                                 @RequestParam("type")String type,
                                 @RequestParam("desc")String desc,
                                 @RequestParam("purpose")String purpose,
                                 @RequestParam("status")String status,
                                 @RequestParam("alarmFrequencyId")String alarmFrequencyId,
                                 @RequestParam("times")String times,
                                 @RequestParam("minutes")String minutes){
        ProcessInforEntity processInforEntity = new ProcessInforEntity();
//        processInforEntity.setId(Integer.parseInt(id));
        processInforEntity.setHostid(Integer.parseInt(hostid));
        processInforEntity.setIp(ip);
        processInforEntity.setCommand(unicodeToString(command));
        //测试用
     //   processInforEntity.setCommand(command);
        processInforEntity.setType(type);
        processInforEntity.setDesc(desc);
        processInforEntity.setPurpose(purpose);
        processInforEntity.setStatus(status);
        processInforEntity.setAlarmFrequencyId((StringUtils.isBlank(alarmFrequencyId))?0:Integer.parseInt(alarmFrequencyId));
        alarmFrequencyEntity.setTimes((StringUtils.isBlank(times))?0:Integer.parseInt(times));
        alarmFrequencyEntity.setMinutes((StringUtils.isBlank(minutes))?0:Integer.parseInt(minutes));

        int info = processInfoDataService.insertProcessInfo(processInforEntity, alarmFrequencyEntity);

        return info;
    }

     /**
      *@Description 批量删除alarm_process
      *@Param
      *@return
      *@Author  ME
      *@Date  2018/6/27 16:00
      *@修改人和其它信息
      */
    //删除
    @RequestMapping(value="/deleteProcess")
    public  int deleteProcessInfo(@RequestParam("id")String id,
                                  @RequestParam("hostid")String hostid,
                                  @RequestParam("ip")String ip,
                                  @RequestParam("command")String command,
                                  @RequestParam("type")String type,
                                  @RequestParam("desc")String desc,
                                  @RequestParam("purpose")String purpose,
                                  @RequestParam("status")String status){
        ProcessInforEntity processInforEntity = new ProcessInforEntity();
        processInforEntity.setId(Integer.parseInt(id));
        processInforEntity.setHostid(Integer.parseInt(hostid));
        processInforEntity.setIp(ip);
        processInforEntity.setCommand(command);
        processInforEntity.setType(type);
        processInforEntity.setDesc(desc);
        processInforEntity.setPurpose(purpose);
        processInforEntity.setStatus(status);

        int dao= processInfoDataService.deleteProcessInfo(processInforEntity);

        return dao;
    }



     /**
      *@Description 更行alarm_process表中信息
      *@Param ProcessInforEntity
      *@return 0，1
      *@Author  zhoujie
      *@Date  2018/6/27 23:00
      *@修改人和其它信息
      */
    @RequestMapping(value="/updateProcess")
    public int updateProcessInfo(@RequestParam("id")String id,
                                 @RequestParam("hostid")String hostid,
                                 @RequestParam("ip")String ip,
                                 @RequestParam("command")String command,
                                 @RequestParam("type")String type,
                                 @RequestParam("desc")String desc,
                                 @RequestParam("purpose")String purpose,
                                 @RequestParam("status")String status,
                                 @RequestParam("alarmFrequencyId")String alarmFrequencyId,
                                 @RequestParam("times")String times,
                                 @RequestParam("minutes")String minutes) {
        ProcessInforEntity processInforEntity = new ProcessInforEntity();
        processInforEntity.setId(Integer.parseInt(id));
        processInforEntity.setHostid(Integer.parseInt(hostid));
        processInforEntity.setIp(ip);
        processInforEntity.setCommand(unicodeToString(command));
        processInforEntity.setType(type);
        processInforEntity.setDesc(desc);
        processInforEntity.setPurpose(purpose);
        processInforEntity.setStatus(status);
        processInforEntity.setAlarmFrequencyId((StringUtils.isBlank(alarmFrequencyId))?0:Integer.parseInt(alarmFrequencyId));
        alarmFrequencyEntity.setTimes((StringUtils.isBlank(times))?0:Integer.parseInt(times));
        alarmFrequencyEntity.setMinutes((StringUtils.isBlank(minutes))?0:Integer.parseInt(minutes));

        int dao = processInfoDataService.updateProcessInfo(processInforEntity, alarmFrequencyEntity);

        return dao;
    }

     /**
      *@Description:查询主机ip详细信息
      *@Param  ProcessInforEntity
      *@return  HostInformation
      *@Author  zhoujie
      *@Date  2018/6/27 23:02
      *@修改人和其它信息
      */
    @RequestMapping(value="/selectHostInfo")
    public String selectHostInfo(@RequestParam("hostid")String hostid){

            List<HostInformation> hostInformations = processInfoDataService.selectHostInfo(Integer.parseInt(hostid));
            if(hostInformations.size()>=1 && hostInformations!=null ){
                String hostStr = JSON.toJSONString(hostInformations);
                return hostStr;
            }

            return null;
    }

    // unicode转化为String
    public  String unicodeToString(String unicode) {

        StringBuffer string = new StringBuffer();

        String[] hex = unicode.split("\\\\u");

        for (int i = 1; i < hex.length; i++) {

            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);

            // 追加成string
            string.append((char) data);
        }

        return string.toString();
    }



}
